﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using ExtAspNet;

namespace HY.WEB.OA
{
    public partial class task : PageBase
    {
        public string whe = "", stype = "";
        public string us, tab = "task";
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            if (!IsPostBack)
            {
               //s btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
                //btnNew.OnClientClick = Window1.GetSaveStateReference(hidstype.ClientID)+ Window1.GetShowReference("hrad_add.aspx", "新增");
                btnSearch.OnClientClick = Window1.GetShowReference("../function/search.aspx?tab="+tab+"", "高级搜索");
                fill();
                BindGrid();
            }
        }

        private void fill()
        {
            DataTable d = BLL.BLLfun.get_search(tab);
            for (int i = 0; i < d.Rows.Count; i++)
            {
                ddlfiled.Items.Add(new ExtAspNet.ListItem(d.Rows[i][1].ToString(), d.Rows[i][0].ToString()));
            }
            ddlfiled.DataBind();

            DataTable e = BLL.BLLfun.get_where();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlwhere.Items.Add(new ExtAspNet.ListItem(e.Rows[i][0].ToString(), e.Rows[i][1].ToString()));
            }
             ddlwhere.DataBind();   ddlwhere.SelectedIndex = ddlwhere.Items.IndexOf(ddlwhere.Items.FindByValue("="));


        }


        private void BindGrid()
        {
            DataTable  table2, table3;

     

            table2 = BLL.oa.task.get_list_y(us,whe);
            Grid2.DataSource = table2;
            Grid2.DataBind();

            table3 = BLL.oa.task.get_list_n(us,whe);
            Grid3.DataSource = table3;
            Grid3.DataBind();

        }

      

        protected void Grid2_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid2.PageIndex = e.NewPageIndex;
        }



        protected void Grid2_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = BLL.oa.task.get_list_y(us,"");
            //DataTable table = BLL.oa.ad.get_list(stype);

            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid2.DataSource = view1;
            Grid2.DataBind();
        }

        protected void Grid3_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid3.PageIndex = e.NewPageIndex;
        }



        protected void Grid3_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {

            DataTable table = BLL.oa.task.get_list_n(us,"");
            //DataTable table = BLL.oa.ad.get_list(stype);


            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);

            Grid3.DataSource = view1;
            Grid3.DataBind();
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            txt.ShowTrigger1 = true;
            string ddlf = ddlfiled.SelectedValue;
            string ddlw = ddlwhere.SelectedValue;
            string key = txt.Text.Trim();
            if (ddlw.Equals("="))
                whe = "" + ddlf + " like  '%" + key + "%'";
            else
            {
                if (ddlf == "sid" || ddlf == "编号")
                    whe = "" + ddlf + " " + ddlw + " " + key + "";
                else
                    whe = "" + ddlf + " " + ddlw + " '" + key + "'";

            }
            if (key == "") whe = "";

            BindGrid();
        }

        /// <summary>
        /// 清除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            txt.Text = String.Empty;
            txt.ShowTrigger1 = false;
            BindGrid();
        }


        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int val = Grid2.PageIndex * Grid2.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid2.SelectedRowIndexArray)
            {
                sb.Append(Grid2.DataKeys[row+val][0].ToString());
                sb.Append(",");

            }

           // Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));
          
            BindGrid();

        }


        protected void btnDelete2_Click(object sender, EventArgs e)
        {
            int val = Grid3.PageIndex * Grid3.PageSize;
            StringBuilder sb = new StringBuilder();
            foreach (int row in Grid3.SelectedRowIndexArray)
            {
                sb.Append(Grid3.DataKeys[row + val][0].ToString());
                sb.Append(",");

            }

            // Alert.ShowInTop("你选择了删除项：" + sb.ToString().TrimEnd(','));
            BLL.Bllbase.DeleteList(tab, sb.ToString().TrimEnd(','));

            BindGrid();

        }


        protected void Grid2_RowCommand(object sender, ExtAspNet.GridCommandEventArgs e)
        {
            if (e.CommandName == "edit") //修改 
            {
                string DD = Grid2.DataKeys[e.RowIndex][0].ToString();
               DataTable dt1= BLL.oa.task.get_list(us,"sid="+DD+"");
                   // ExtAspNet.Alert.Show(DD);
               hidsid.Text = dt1.Rows[0]["sid"].ToString().Trim();
                string dts=dt1.Rows[0]["dt"].ToString().Trim();
                DateTime lastday = DateTime.Parse(dts);
                
                date1.Text = lastday.ToString("yyyy-M-d HH:mm:ss");             

                txtmemo.Text = dt1.Rows[0]["tmemo"].ToString().Trim();

             }
            else if (e.CommandName == "Del")//删除 
            {

            } 

        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            // Alert.ShowInTop("弹出窗口关闭了！");
            string key = txt.Text.Trim();
          //  stype = hidstype.Text;
            try
            {
                if (key == "") whe = BLL.pub.pubwhere.get_where_sql(Session["ID"].ToString(), "task");
                BindGrid();

            }
            catch (Exception ex)
            {
                Alert.Show("查询条件错误");
            }
            finally
            {
                whe = "";
            }
        }
        protected void btsave_Click(object sender, EventArgs e)
        {
            string user=us;
            string dt=date1.Text.Trim();
            string memo=txtmemo.Text.Trim();
            string hid = hidsid.Text;

            if (memo!=""&hid=="")
             BLL.oa.task.ins_task(user,dt,memo);
          
            if (memo != "" & hid != "")
                BLL.oa.task.ups_task(hid,user,dt,memo);
            hidsid.Text = "";
            txtmemo.Text = "";
            BindGrid();
        }

    
    }
}
